#include"CalServer.hpp"
#include<memory>

using namespace server;
using namespace std;

static void usage(std::string proc)
{
    std::cout << "\nUsage:\n\t" << proc << " local_port\n\t\n";
}

//req里面一定是我们处理好的完整的请求对象！
//resp根据req进行业务处理填充resp,不用管任何的IO和序列化和反序列化等任何细节！
bool cal(const Request& req,Respone& resp)
{
    //我们已经有了结构化的数据了可以直接使用！
    resp.exitcode_ = 0;
    resp.result_ = 0;
    //初始一下

    switch(req.op_)
    {
        case '+':
            resp.result_ = req.x_ + req.y_;
            break;
        case '-':
            resp.result_ = req.x_ - req.y_;
            break;
        case '*':
            resp.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if(req.y_ == 0)
            {
                resp.exitcode_ = DIV_ZERO;
                break;
            }
            resp.result_ = req.x_ / req.y_;
            break;
        }
        case '%':
        {
            if(req.y_ == 0)
            {
                resp.exitcode_ = MOD_ZERO;
                break;
            }
            resp.result_ = req.x_ % req.y_;
            break;
        }
        default:
            resp.exitcode_ = OP_ERR;
            break;
    }

    return true;
}
int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);//将字符串转换为整数


    unique_ptr<TcpServer> tsvr(new TcpServer());
    tsvr->initServer();
    tsvr->start(cal);

    return 0;
}
